<template>
  <el-card style="width: 100%" class="mt-10">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!cartgroyStroe.cartgroy3Id"
      @click="showAddAttr"
    >
      添加属性
    </el-button>
    <el-table
      :data="attrList"
      border
      style="width: 90%"
      class="mt-20"
      v-loading="loading"
    >
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="属性名称" prop="attrName" width="150" />
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
            class="attr-tag"
          >
            {{ attrValue.valueName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" width="120" label="操作">
        <template #default="{ row }">
          <el-tooltip effect="dark" content="修改属性" placement="bottom-end">
            <el-button
              size="small"
              type="primary"
              :icon="Edit"
              @click="setAttrItem(row)"
            />
          </el-tooltip>
          <el-tooltip effect="dark" content="删除属性" placement="bottom-end">
            <span class="attr-btn">
              <el-popconfirm
                :title="`确定删除${row.attrName}吗?`"
                @confirm="deleteAttrItem(row.id)"
              >
                <template #reference>
                  <el-button size="small" type="danger" :icon="Delete" />
                </template>
              </el-popconfirm>
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { watch, ref, onMounted } from "vue";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { useCartgroyStroe } from "@/stores/cartgroy";
import { useAttrStore } from "@/stores/attr";
import { getAttrListApi, deleteAttrItemApi } from "@/api/product/attr";
import type { AttrItem, AttrList } from "@/api/product/model/attrModel";

const loading = ref(false);

const cartgroyStroe = useCartgroyStroe();
const attrList = ref<AttrList>([]);

// 接收自定义事件
const emit = defineEmits(["setIsshowAttrList"]);

const getAttrList = async () => {
  loading.value = true;
  const { cartgroy1Id, cartgroy2Id, cartgroy3Id } = cartgroyStroe;
  attrList.value = await getAttrListApi({
    cartgroy1Id: cartgroy1Id as number,
    cartgroy2Id: cartgroy2Id as number,
    cartgroy3Id: cartgroy3Id as number,
  });
  loading.value = false;
};
/******************************* 请求分类数据展示列表 **********************************/
// onMounted(() => {
//   if (!cartgroyStroe.cartgroy3Id) return;
//   getAttrList();
// });

watch(
  () => cartgroyStroe.cartgroy3Id,
  (newVal) => {
    if (!newVal) {
      attrList.value = [];
      return;
    }
    getAttrList();
  },
  {
    immediate: true,
  }
);

// 点击添加属性跳转
const showAddAttr = () => {
  attrStore.$reset();
  emit("setIsshowAttrList", false);
};

/******************************* 删除某一项 **********************************/
const deleteAttrItem = (id: number) => {
  deleteAttrItemApi(id);
  getAttrList();
};

/******************************* 修改某一项 **********************************/
const attrStore = useAttrStore();
const setAttrItem = (row: AttrItem) => {
  emit("setIsshowAttrList", false);
  attrStore.$patch({
    id: row.id,
    attrName: row.attrName,
    attrValueList: row.attrValueList,
  });
};
</script>

<style scoped>
.attr-tag {
  margin-right: 10px;
}
.attr-btn {
  margin-left: 10px;
}
</style>
